﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using FluidKit.Controls;
using System.Windows.Threading;

namespace ImageView
{
	/// <summary>
	/// Interaction logic for View3D.xaml
	/// </summary>
	public partial class View3D : UserControl, IViev
	{

        Thread T;
        private string[] _imgs;

		public View3D()
		{
			this.InitializeComponent();
            EFlow.Layout = new CoverFlow();
            VCFlow.IsChecked = true;
		}

        public void ClearItems()
        {
            EFlow.Items.Clear();
        }

        public int SelectedIndex
        {
            get { return EFlow.SelectedIndex; }
        }

        private void DoEvents()
        {
            DispatcherFrame f = new DispatcherFrame();
            Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
            (SendOrPostCallback)delegate(object arg)
            {
                DispatcherFrame fr = arg as DispatcherFrame;
                fr.Continue = false;
            }, f);
            Dispatcher.PushFrame(f);
        }

        private void ThreadFnc()
        {
            int i = 0;

            this.Dispatcher.Invoke((Action)delegate
            {
                App.Taskbar.SetTaskbarProgessState(Microsoft.WindowsAPICodePack.Taskbar.TaskbarProgressBarState.Indeterminate);
                LoadProgress.IsIndeterminate = true;
            });
            foreach (var img in _imgs)
            {
                this.Dispatcher.Invoke((Action)delegate
                {
                    Img3D im = new Img3D();
                    im.VideoIconVisible = App.MainWin.IsVideo(img);
                    im.ThumbImage = App.Shell.GetThumbnail(img);
                    im.SetRotation(img);
                    EFlow.Items.Add(im);
                    ++i;
                    if (i == 5)
                    {
                        DoEvents();
                        i = 0;
                    }
                });
            }
            this.Dispatcher.Invoke((Action)delegate
            {
                App.Taskbar.SetTaskbarProgessState(Microsoft.WindowsAPICodePack.Taskbar.TaskbarProgressBarState.NoProgress);
                LoadProgress.IsIndeterminate = false;
            });
        }

        public void Fill(string[] items)
        {
            _imgs = items;
            ImageCount.Text = _imgs.Length.ToString();
            T = new System.Threading.Thread(ThreadFnc);
            T.SetApartmentState(ApartmentState.STA);
            T.Start();
        }

        public void TerminateThread()
        {
            if (T != null)
            {
                if (T.IsAlive) T.Abort();
            }
        }

        private void BtnMinus_Click(object sender, RoutedEventArgs e)
        {
            int newi = EFlow.SelectedIndex - 1;
            if (newi > -1) EFlow.SelectedIndex = newi;
        }

        private void BtnPlus_Click(object sender, RoutedEventArgs e)
        {
            int newi = EFlow.SelectedIndex + 1;
            if (newi < EFlow.Items.Count) EFlow.SelectedIndex = newi;
        }

        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            RadioButton s = (RadioButton)sender;
            switch (s.Content.ToString())
            {
                case "Slide Deck":
                    EFlow.Layout = new SlideDeck();
                    break;
                case "Cover Flow":
                    EFlow.Layout = new CoverFlow();
                    break;
                case "Time Machine":
                    EFlow.Layout = new TimeMachine2();
                    break;
                case "VForm":
                    EFlow.Layout = new VForm();
                    break;
                case "Rolodex":
                    EFlow.Layout = new Rolodex();
                    break;
            }
        }
    }
}